<template>
  <div class="class_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt=""/>
      </div>
      <!--      这里不是v-model-->
      <el-form :model="loginForm" :rules="loginRules" label-width="0px" class="input_box" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input show-password v-model="loginForm.password" prefix-icon="iconfont icon-mima1"></el-input>
        </el-form-item>

        <el-form-item class="btn_box">
          <el-button @click="login" type="primary">登录</el-button>
          <el-button @click="reset" type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [{
          required: true,
          message: '请输入账号',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        },
          {
            min: 3,
            message: '长度在 3 个及以上',
            trigger: 'blur'
          }]
      }
    }
  },
  methods: {
    reset () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async validate => {
          if (validate) {
            const { data } = await this.$http.get('vueUser/user/login', {
              params: this.loginForm
            })

            if (!data.success) {
              return this.$message.error(data.message)
            }
            window.sessionStorage.setItem('token', data.result.token)
            this.$router.push('/home')
          }
        }
      )

      /*            this.$refs.loginFormRef.validate(validate =>{

                      if(validate){
                        this.$http.get('vueUser/user/login',{
                          params: this.loginForm
                        }).then(resp =>{
                          console.log(resp)
                        })
                      }
                    }
                  );*/
    }
  }
}
</script>

<style lang="less" scoped>
.class_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50px);
  background-color: #fff;

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}

.input_box {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btn_box {
  display: flex;
  justify-content: flex-end;
}
</style>
